let mysql = require('mysql')
let axios = require('axios')
let cheerio = require('cheerio')

let con = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '1234',
    database: 'bk'
})
con.connect(() => {
    console.log('数据库连接成功')
})

//延时函数
function delay(count = 2000) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve()
        }, count)
    })
}

async function getPageUrl(num) {
    try {
        let httpUrl = 'https://sobooks.cc/page/' + num
        let res = await axios.get(httpUrl, { timeout: 6000 })
        let $ = cheerio.load(res.data)
        console.log('请求主页面成功')
        $('#cardslist .card-item .thumb-img>a').each((i, el) => {
            let href = $(el).attr('href')
                ; (async function () {
                    await delay(i * 500)
                    getBookInfo(href)
                })()
        })
        return delay(10000)
    } catch (err) {
        console.log(num + '主页面出错')
    }
}
async function getBookInfo(href) {
    try {
        let res = await axios({
            url: href,
            method: 'post',
            headers: {
                "authority": 'sobooks.cc',
                'method': 'POST',
                'path': '/books/18739.html',
                'scheme': 'https',
                'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                // 'accept-encoding': 'gzip, deflate, br',
                'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
                'cache-control': 'max-age=0',
                'content-length': "19",
                'content-type': ' application/x-www-form-urlencoded',
                'cookie': 'UM_distinctid=17b77e7a323df-0d0faed80378e1-7a697f6c-e1000-17b77e7a324c1; CNZZDATA1259444303=689317406-1629800943-https%253A%252F%252Fcn.bing.com%252F%7C1630200794',
                'origin': ' https://sobooks.cc',
                'referer': 'https://sobooks.cc/books/18739.html',
                'sec-ch-ua': ' " Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"',
                'sec-ch-ua-mobile': '?0',
                'sec-fetch-dest': 'document',
                'sec-fetch-mode': 'navigate',
                'sec-fetch-site': 'same-origin',
                'sec-fetch-user': '?1',
                'upgrade-insecure-requests': "1",
                'user-agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36 Edg/91.0.864.71'
            },
            data: "e_secret_key=889272",
            timeout:6000
        })
        console.log('请求分页面成功')
        let $ = cheerio.load(res.data)
        let bookimg = $('.article-content .bookpic img').attr('src')
        let bookname = $('.article-content .bookinfo li:nth-child(1)').text()
        bookname = bookname.slice(3,).trim()
        let author = $('.article-content .bookinfo li:nth-child(2)').text()
        author = author.slice(3,).trim()
        let geshi = $('.article-content .bookinfo li:nth-child(3)').text()
        let tag = $('.article-content .bookinfo li:nth-child(4)').text()
        tag = tag.slice(3,)
        let pubtime = $('.article-content .bookinfo li:nth-child(5)').text()
        pubtime = pubtime.slice(3,)
        let score = $('.article-content .bookinfo li:nth-child(6) b').attr("class")
        score = score[score.length - 1]
        let cataory = $('#mute-category > a').text().trim()
        let abrief = $('body>section>div.content-wrap>div>article>p:nth-child(8)').text()
        let bbrief = $(".article-content").html()
        let bookUrl = href
        let download = $('.e-secret b a:last-child').attr('href').split('?url=')[1].trim()
        let arr = [bookimg, bookname, author, geshi, tag, pubtime, score, cataory, abrief, bbrief, bookUrl, download]

        let strSql = 'insert into book(bookimg,bookname,author,geshi,tag,pubtime,score,cataory,abrief,bbrief,bookUrl,download) values(?,?,?,?,?,?,?,?,?,?,?,?)'
        con.query(strSql, arr)
    }
    catch (err) {
        console.log('分页面出错')
    }
}







; (async function () {
    for (let index = 1; index <= 300; index++) {

        console.log(index + '页')
        await getPageUrl(index)
    }
})()